CREATE TABLE [TRB_IPTU_SERIE] (
	[IPS_COD] [int] IDENTITY (1, 1) NOT NULL ,
	[IPS_USUARIO] [varchar] (50) NOT NULL,
	[IPS_DATA_LANCAMENTO] [datetime] NOT NULL,
	[IPS_DATA_VENCIMENTO] [datetime] NOT NULL,
	[IPS_DECONTO] [bit] NOT NULL,
	[IPS_APENAS_UNICA] [bit] NOT NULL,
	[IPS_EXERCICIO] [int] NOT NULL,
	[IPS_INSCRICAO] [varchar] (30) NOT NULL,
	CONSTRAINT [PK_TRB_IPTU_SERIE] PRIMARY KEY  CLUSTERED 
	(
		[IPS_COD]
	)  ON [PRIMARY] 
) ON [PRIMARY]
GO

alter table TRB_IPTU_SERIE alter column IPS_DECONTO char(1)
alter table TRB_IPTU_SERIE alter column IPS_APENAS_UNICA char(1)

select * from trb_imovel where imv_inscricao like '4031400367%'


select ses_usuario from  fr_sessao_vi


select * from trb_imovel a join trb_iptu b on b.imv_cod = a.imv_cod 
where imv_inscricao like '4031400367%'

select * from trb_imovel a join trb_iptu b on b.imv_cod = a.imv_cod 
where imv_inscricao like '4031400367%'

select a.* from trb_imovel a join trb_iptu b on b.imv_cod = a.imv_cod 
where imv_inscricao like '4031400367[A-Z]%'


select * from trb_baixa where lan_cod in (select lan_cod from trb_imovel a join trb_iptu b on b.imv_cod = a.imv_cod where imv_inscricao like '4031400367%')

select x.* from trb_lancamento l
join trb_baixa x on x.lan_cod = l.lan_cod
where tla_cod in (select tla_cod from trb_imovel a join trb_iptu b on b.imv_cod = a.imv_cod where imv_inscricao like '4031400367%' and exe_ano = 2008)


select * from TRB_IPTU_SERIE



select 
	b.tla_cod 
	-- min(b.tla_cod), max(b.tla_cod)
from trb_imovel 	a 
join trb_iptu 		b	on b.imv_cod = a.imv_cod 
join trb_lancamento c 	on c.tla_cod = b.tla_cod
where imv_inscricao like '4031400367%' 


select 
	b.tla_cod, d.lan_cod, bxa_cod
from trb_imovel 	a 
join trb_iptu 		b	on b.imv_cod = a.imv_cod 
join trb_lancamento c 	on c.tla_cod = b.tla_cod
join trb_baixa 		d 	on d.lan_cod = c.lan_cod
where imv_inscricao like '4031400367%' 
and exe_ano = 2008

select 
	b.tla_cod, d.lan_cod, bxa_cod
from trb_imovel 	a 
join trb_iptu 		b	on b.imv_cod = a.imv_cod 
join trb_lancamento c 	on c.tla_cod = b.tla_cod
join trb_baixa 		d 	on d.lan_cod = c.lan_cod
where c.lan_cod not in (1359011, 1385875)
and	imv_inscricao like '4031400367%' 
and exe_ano = 2008


-- tla_cod = 3209518 -- baixados

select * from trb_lancamento l
	where 
		lan_cod not in (1359011, 1385875)
		and
		exists ( select 1 from trb_imovel 	a
					join trb_iptu 			b	on b.imv_cod = a.imv_cod 
					join trb_lancamento 	c 	on c.tla_cod = b.tla_cod
					where imv_inscricao like '4031400367%' 
					and exe_ano = 2008
					and c.lan_cod = l.lan_cod
				 	)

select distinct 'delete from trb_lancamento where tla_cod = ' + cast(tla_cod as sysname) 
	from trb_lancamento l
	where 
		lan_cod not in (1359011, 1385875)
		and
		exists ( select 1 from trb_imovel 	a
					join trb_iptu 			b	on b.imv_cod = a.imv_cod 
					join trb_lancamento 	c 	on c.tla_cod = b.tla_cod
					where imv_inscricao like '4031400367%' 
					and exe_ano = 2008
					and c.lan_cod = l.lan_cod
				 	)



select distinct 'delete from trb_iptu where tla_cod = ' + cast( b.tla_cod as sysname) 
	from trb_imovel 	a
	join trb_iptu 			b	on b.imv_cod = a.imv_cod 
	where imv_inscricao like '4031400367%' 
	and exe_ano = 2008
